<template>
  <div class="main">
    <el-input
      placeholder="单位人 / 回复"
      v-model="search"
      style="width:302px;margin-bottom:20px"
      @keyup.native.enter="searchcon('1')"
    >
      <i
        slot="suffix"
        class="el-input__icon el-icon-search"
        style="cursor:pointer"
        @click="searchcon('1')"
      ></i>
    </el-input>

    <!-- 下面是表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{background:'rgba(0,104,183,1)',color:'rgba(255,255,255,1)'}"
    >
      <el-table-column prop="department_name" label="单位" width="180"></el-table-column>
      <el-table-column prop="readstatus" label="是否回复" width="180"></el-table-column>
      <el-table-column prop="from_user_name" label="最新回复人"></el-table-column>
      <el-table-column prop="replaytime" label="最新回复时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope" style>
          <i
            class="el-icon-chat-line-round"
            style="margin-right:10px;cursor:pointer;font-size:30px"
            @click="jump(scope.row)"
          ></i>
          <i
            class="el-icon-delete-solid"
            style="cursor:pointer;font-size:30px"
            @click="deletmsg(scope.row)"
          ></i>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align:center;margin-top:25px">
      <div class="block">
             <el-pagination
          :page-size="per_page-0"
          layout="total,  prev, pager, next, jumper"
          :total="total-0"
          :current-page="current_page-0"
          @current-change="twochange"
        ></el-pagination>
      </div>
    </div>

    <!-- 删除提醒 -->
    <el-dialog title="删除提示" :visible.sync="dialogVisible" width="30%">
      <span>确认删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="suredelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getmsg('1');
  },
  data() {
    return {
      search_con: "",
      tableData: [

      ],
      dialogVisible: false,
      // 搜索的数据绑定
      search: "",
      // 删除的临时id
      deleteid: "",
      // 消息总条数
      total: "0",
      // 每页多少条
      per_page: "0",
      // 当前多少页
      current_page: "1",
      // 两个男孩
      twoboy:'1'
    };
  },
  methods: {
    // 跳到聊天界面
    jump(row) {
      let id = row.from_user_id;
      window.sessionStorage.talkname = row.from_user_name;
      window.sessionStorage.talkid = id;
      console.log(row);
      this.$router.push("/talk");
    },
    //开头获取数据的函数
    async getmsg(page){
      this.twoboy=1;
      var msg = await this.$http.get("admin/getallmsg?page="+page);
      console.log(msg.data.data)
      this.tableData = msg.data.data.data;
      var len = msg.data.data.data.length;
      for(let i=0;i<len;i++){
        if(this.tableData[i].readstatus==1){
          this.tableData[i].readstatus='未回复'
        }else if(this.tableData[i].readstatus==0){
           this.tableData[i].readstatus='已回复'
        }else{
          this.tableData[i].readstatus='未知'
        }
      }
       this.current_page = msg.data.data.current_page;
        this.total = msg.data.data.total;
        this.per_page = msg.data.data.per_page;
    },
    // 搜索的函数
    async searchcon(page) {
      let con = this.search.trim();
      if (con == "") {
        this.getmsg('1');
      } else {
        let msg = await this.$http.get("admin/searchmsg?page="+page,{
          params:{
            keywords:con
          }
        })
        console.log(msg)
        if(msg.data.msg=="无任何消息！"){
          this.$message.error("无搜索内容！")
        }else{
           this.twoboy=2;
           this.tableData = msg.data.data.data;
                 var len = msg.data.data.data.length;
      for(let i=0;i<len;i++){
        if(this.tableData[i].readstatus==1){
          this.tableData[i].readstatus='未回复'
        }else if(this.tableData[i].readstatus==0){
           this.tableData[i].readstatus='已回复'
        }else{
          this.tableData[i].readstatus='未知'
        }
      }
       this.current_page = msg.data.data.current_page;
        this.total = msg.data.data.total;
        this.per_page = msg.data.data.per_page;
        }
      }
    },
    // 删除的函数
    deletmsg(row) {
      this.deleteid = row.from_user_id;
      this.dialogVisible = true;
    },
    // 确认删除的函数
    async suredelete() {
    let msg = await this.$http.delete("admin/delmsg",{
       params:{
fromuser_id:this.deleteid-0
       }
     })
     console.log(msg)
     if(msg.data.msg=="删除失败！"){
       this.$message.error("删除失败！")
     }else{
   this.$message.success("删除成功！")
       this.dialogVisible = false;
   if (this.twoboy == 1) {
        this.getmsg(this.current_page);
      } else {
        this.searchcon(this.current_page);
      }
     }
    },
       // 换页的函数
    twochange(val) {
      if (this.twoboy == 1) {
        this.getmsg(val);
      } else {
        this.searchcon(val);
      }
    }
  }
};
</script>


<style scoped>
.main {
  min-height: 260px;
  width: 1280px;
  margin: 20px auto 0;
}
</style>